/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.max.oti.system.dao.hibernate;

import com.max.oti.system.dao.interfac.IPracticeDao;
import com.max.oti.system.dao.util.GenericEntityDao;
import com.max.oti.system.model.Practice;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Hibernate;

/**
 *
 * @author Alberto
 */
public class PracticeDao extends GenericEntityDao<Practice, Long> implements IPracticeDao{
    @Override
    public Practice load(Serializable id) throws Exception {
        Practice practice = (Practice) getSessionFactory()
                .getCurrentSession()
                .createQuery("FROM Practice p Where p.id=" + id)
                .uniqueResult();
        
        for (int i = 0; i < practice.getFunctionPracticeList().size(); i++) {
            Hibernate.initialize(practice.getFunctionPracticeList().get(i));            
        }
        if(!practice.getHistoryPracticeList().isEmpty()){
            Hibernate.initialize(practice.getHistoryPracticeList().get(0));
        }
        
        return practice;
    }
    
    @Override
    public Practice getById(Serializable id) throws Exception {
        Practice practice = (Practice) getSessionFactory()
                .getCurrentSession()
                .createQuery("FROM Practice p Where p.id=" + id)
                .uniqueResult();
        for (int i = 0; i < practice.getFunctionPracticeList().size(); i++) {
            Hibernate.initialize(practice.getFunctionPracticeList().get(i));            
        }
        for (int i = 0; i < practice.getHistoryPracticeList().size(); i++) {
            Hibernate.initialize(practice.getHistoryPracticeList().get(i));            
        }
        for (int i = 0; i < practice.getSupervisionPracticeList().size(); i++) {
            Hibernate.initialize(practice.getSupervisionPracticeList().get(i));            
        }
        for (int i = 0; i < practice.getReportPracticeList().size(); i++) {
            Hibernate.initialize(practice.getReportPracticeList().get(i));            
        }
        for (int i = 0; i < practice.getAmplificationPracticeList().size(); i++) {
            Hibernate.initialize(practice.getAmplificationPracticeList().get(i));            
        }
        for (int i = 0; i < practice.getSustentationPracticeList().size(); i++) {
            Hibernate.initialize(practice.getSustentationPracticeList().get(i));            
        }
        return practice;
    }
    
    
    @Override
    public List listHQL(String hql, int lazyChargeOption) throws Exception {
        List list = new ArrayList();
        if(lazyChargeOption == 1){
            list = getSessionFactory().getCurrentSession()
                    .createQuery(hql).list();
            for (int i = 0; i < list.size(); i++) {
                Practice p = (Practice) list.get(i);
                for (int j = 0; j < p.getHistoryPracticeList().size(); j++) {
                    Hibernate.initialize(p.getHistoryPracticeList().get(j));                   
                }                
                for (int j = 0; j < p.getFunctionPracticeList().size(); j++) {
                    Hibernate.initialize(p.getFunctionPracticeList().get(j));
                }
            }
        }
        return list;
    }
    
    
    
}
